<template>
  <div class="container">
    <!-- 头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>广告</el-breadcrumb-item>
      <el-breadcrumb-item>广告内容</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="$router.push('/home/adv/content/add')">添加广告</el-button>
      <el-table :data="advContentList" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="广告名称" prop="advname" width="300"></el-table-column>
        <el-table-column label="广告图片">
          <template v-slot="scope">
            <img
              v-for="(item, i) in scope.row.path"
              :key="i"
              :src="baseURL + item"
              alt=""
              class="advImg"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template v-slot="scope">
            <el-button
              type="primary"
              size="mini"
              round
              @click="$router.push(`/home/adv/content/edit/${scope.row.id}`)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" round @click="removeAdvContentById(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'AdvContent',
  data() {
    return {
      // 广告内容列表
      advContentList: [],
      // 默认图片路径
      baseURL: this.baseURL + 'upload/'
    }
  },
  created() {
    this.getAdvContentList()
  },
  methods: {
    // 获取广告内容列表
    async getAdvContentList() {
      const { data: res } = await this.$http.get('/admin/advcontent')
      this.advContentList = res.data
    },
    // 根据id删除广告内容
    removeAdvContentById(row) {
      this.$confirm('确定是否删除广告内容', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`/admin/advcontent/${row.id}`)
          if (res.status !== 200) return this.$message.error(res.msg)
          this.$message.success(res.msg)
          // 重新获取数据
          this.getAdvContentList()
        })
        .catch(() => this.$message.info('取消删除'))
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 20px;
}
.advImg {
  width: 80px;
  height: 50px;
  vertical-align: middle;
  object-fit: cover;

  & ~ .advImg {
    margin-left: 10px;
  }
}
</style>
